package com.chexiehui.jc.mvp.car;

import com.chexiehui.jc.Constants;
import com.chexiehui.jc.base.ApiCallback;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BasePage;
import com.chexiehui.jc.base.BasePresenter;
import com.chexiehui.jc.entity.ShopInfo;
import com.chexiehui.jc.entity.SpecialCarInfo;

import java.util.List;

/**
 * @Description 店铺详情
 * @Auth SongJun
 * @Date 2020/05/21 00:18
 **/
public class ShopDetailsPresenter extends BasePresenter<ShopDetailsCovenant.View, ShopDetailsCovenant.Stores>
        implements ShopDetailsCovenant.Presenter {

    public ShopDetailsPresenter(ShopDetailsCovenant.View view) {
        attachView(view);
    }

    @Override
    public void getShopInfo() {
        addSubscription(appStores.getShopInfo(mvpView.getShopId()),
                new ApiCallback<BaseModel<ShopInfo>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<ShopInfo> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null) {
                                mvpView.onGetShopInfoSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "暂无门店信息");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetShopInfoFailure(new BaseModel<>(code, msg));
                    }
                });
    }

    @Override
    public void getCarList(int pageIndex) {
        addSubscription(appStores.getCarList(mvpView.getShopId(), pageIndex, 10),
                new ApiCallback<BaseModel<BasePage<SpecialCarInfo>>>(mvpView) {
                    @Override
                    public void onSuccess(BaseModel<BasePage<SpecialCarInfo>> model) {
                        if (model.getResultCode() == 0) {
                            if (model.getData() != null && model.getData().getItems() != null && model.getData().getItems().size() > 0) {
                                mvpView.onGetCarListSuccess(model);
                            } else {
                                onFailure(Constants.PAGE_DATA_EMPTY, "没有找到任何数据");
                            }
                        } else {
                            onFailure(model.getResultCode(), model.getResultInfo());
                        }
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.onGetCarListFailure(new BaseModel<>(code, msg));
                    }
                });
    }

}